const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const fs = require('fs');

// 拿出commonjs模块
const nodeModules = {};
fs.readdirSync('node_modules')
  .filter( (catalogue) => {
    return ['.bin'].indexOf(catalogue) === -1;
  })
  .forEach( (mod) => {
    nodeModules[mod] = 'commonjs ' + mod;
  });

module.exports = {
  // mode: 'development',
  mode: 'production',
  entry: './src/index.js',
  target: 'node',
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'lib'),
    libraryTarget: 'commonjs2',
  },
  externals: nodeModules,
  node: {
    __filename: false,
    __dirname: false,
    path: 'empty',
    fs: 'empty',
    child_process: 'empty',
  },
  optimization: {
    minimize: true,
  },
  context: __dirname,
  module: {
    noParse: /node-ssh/,
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                [
                  '@babel/preset-env',
                  {
                    targets: {
                      node: true,
                    },
                  },
                ],
              ],
            },
          },
          {
            loader: 'eslint-loader',
            options: {
              formatter: require('eslint-friendly-formatter')
            }
          }
        ],
      },
    ],
  },
  plugins: [new CleanWebpackPlugin()],
}
